package com.amarsoft.are.lang;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateX
{
  public static final int YEAR = 0;
  public static final int YE = 0;
  public static final int MONTH = 1;
  public static final int MO = 1;
  public static final int DATE = 2;
  public static final int DA = 2;
  public static final int HOUR = 3;
  public static final int HO = 3;
  public static final int MINUTE = 4;
  public static final int MI = 4;
  public static final int SECOND = 5;
  public static final int SE = 5;
  public static final int MILLISECOND = 6;
  public static final int MS = 6;
  private Date date;
  private int[] fields;
  private Calendar calendar;

  public DateX(Object paramObject)
  {
    this.date = null;
    this.fields = null;
    this.calendar = null;
    this.date = castDate(paramObject);
    this.calendar = Calendar.getInstance();
    this.calendar.setTime(this.date);
    setFields();
  }

  public DateX()
  {
    this(new Date());
  }

  public DateX(int paramInt1, int paramInt2, int paramInt3)
  {
    this.date = null;
    this.fields = null;
    this.calendar = null;
    this.calendar = Calendar.getInstance();
    this.calendar.clear();
    this.calendar.set(paramInt1, paramInt2 - 1, paramInt3);
    this.date = this.calendar.getTime();
    setFields();
  }

  private void setFields()
  {
    this.fields = new int[7];
    this.fields[0] = this.calendar.get(1);
    this.fields[1] = (this.calendar.get(2) + 1);
    this.fields[2] = this.calendar.get(5);
    this.fields[3] = this.calendar.get(10);
    this.fields[4] = this.calendar.get(12);
    this.fields[5] = this.calendar.get(13);
    this.fields[6] = this.calendar.get(14);
  }

  public int getField(int paramInt)
  {
    return this.fields[paramInt];
  }

  public int getYear()
  {
    return this.fields[0];
  }

  public int getMonth()
  {
    return this.fields[1];
  }

  public int getDate()
  {
    return this.fields[2];
  }

  public int getHour()
  {
    return this.fields[3];
  }

  public int getMinute()
  {
    return this.fields[4];
  }

  public int getSecond()
  {
    return this.fields[5];
  }

  public int getMillisecond()
  {
    return this.fields[6];
  }

  public int[] getAllFields()
  {
    return ((int[])this.fields.clone());
  }

  public boolean isEndMonthOfYear()
  {
    return (this.fields[1] == 12);
  }

  public boolean isEndDayOfMonth()
  {
    int i = 0;
    switch (this.fields[1])
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
      i = (this.fields[2] == 31) ? 1 : 0;
      break;
    case 4:
    case 6:
    case 9:
    case 11:
      i = (this.fields[2] == 30) ? 1 : 0;
      break;
    case 2:
      i = (this.fields[2] == 28) ? 1 : (isLeapYear()) ? 0 : (this.fields[2] == 29) ? 1 : 0;
    }
    return i==1?true:false;
  }

  public boolean isLeapYear()
  {
    return (this.fields[0] % 400 == 0);
  }

  public Date getAdjustRelativeDate(int paramInt1, int paramInt2, boolean paramBoolean)
  {
    if (paramInt2 == 0)
      return this.date;
    if ((paramInt1 < 0) || (paramInt1 > 6))
      return null;
    Calendar localCalendar = Calendar.getInstance();
    localCalendar.setTime(this.date);
    localCalendar.add(getCalendarConst(paramInt1), paramInt2);
    if ((paramBoolean) && (isEndDayOfMonth()))
    {
      int i = localCalendar.get(2);
      localCalendar.set(localCalendar.get(1), i + 1, 1);
      while (localCalendar.get(2) != i)
        localCalendar.add(5, -1);
    }
    return localCalendar.getTime();
  }

  public Date getAbsoluteRelativeDate(int paramInt1, int paramInt2)
  {
    if (paramInt2 == 0)
      return this.date;
    if ((paramInt1 < 0) || (paramInt1 > 6))
      return null;
    Calendar localCalendar = Calendar.getInstance();
    localCalendar.setTime(this.date);
    if (paramInt1 == 1)
      localCalendar.set(getCalendarConst(paramInt1), this.fields[1] + paramInt2 - 1);
    else
      localCalendar.set(getCalendarConst(paramInt1), this.fields[paramInt1] + paramInt2);
    return localCalendar.getTime();
  }

  private int getCalendarConst(int paramInt)
  {
    if (paramInt == 0)
      return 1;
    if (paramInt == 1)
      return 2;
    if (paramInt == 2)
      return 5;
    if (paramInt == 3)
      return 11;
    if (paramInt == 4)
      return 12;
    if (paramInt == 5)
      return 13;
    if (paramInt == 6)
      return 14;
    return paramInt;
  }

  public Date getTime()
  {
    return this.date;
  }

  public String getDateString()
  {
    return getDateString("yyyy/MM/dd");
  }

  public String getDateString(String paramString)
  {
    return new SimpleDateFormat(paramString).format(this.date);
  }

  public static String format(Date paramDate)
  {
    return format(paramDate, "yyyy/MM/dd");
  }

  public static String format(Date paramDate, String paramString)
  {
    return new SimpleDateFormat(paramString).format(paramDate);
  }

  public static Date today()
  {
    return new Date();
  }

  public static Date castDate(Object paramObject)
  {
    Date localDate = null;
    if (paramObject instanceof Date)
      localDate = (Date)paramObject;
    else if (paramObject instanceof DateX)
      localDate = ((DateX)paramObject).getTime();
    else if (paramObject instanceof DataElement)
      localDate = ((DataElement)paramObject).getDate();
    else if (paramObject instanceof Element)
      localDate = castDate(((Element)paramObject).getValue());
    else if (paramObject instanceof Number)
      localDate = new Date(((Number)paramObject).longValue());
    else
      localDate = StringX.parseDate(paramObject.toString());
    return localDate;
  }
}